package com.lcxw.hxzb.friendcircle.framework.redisSchedl;


import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdGameOrder;
import com.lcxw.hxzb.friendcircle.project.system.mapper.YwpdMemberMapper;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdGameOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * 监听所有db的过期事件
 *
 * @author Admin
 */
@Component
public class RedisKeyListener extends KeyExpirationEventMessageListener {


    public RedisKeyListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Autowired
    private IYwpdGameOrderService ywpdGameOrderService;

    @Autowired
    private YwpdMemberMapper ywpdMemberMapper;
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void onMessage(Message message, byte[] pattern) {
        System.out.println("++++++++++++++++++++"+message.toString());
        String mes = message.toString();
        //陪玩者未点击同意 订单超时
        if (mes.startsWith("gameOrder")) {
            Long orderId = Long.valueOf(mes.substring(9));
            //同时要把用户支付的陌陌币还给用户
            YwpdGameOrder ywpdGameOrder=ywpdGameOrderService.selectYwpdGameOrderById(orderId);
            if((ywpdGameOrder.getStatus()+"").equals(2001+"")){
                ywpdMemberMapper.updateCoinByUserId(ywpdGameOrder.getTotal().intValue(),ywpdGameOrder.getUserId());
                YwpdGameOrder gameOrder = new YwpdGameOrder();
                gameOrder.setOrderId(orderId);
                gameOrder.setStatus(4001L);
                ywpdGameOrderService.updateYwpdGameOrder(gameOrder);
            }
        }
        //订单结束
        if (mes.startsWith("gameStart")) {
            Long orderId = Long.valueOf(mes.substring(9));
            YwpdGameOrder gameOrder = new YwpdGameOrder();
            gameOrder.setOrderId(orderId);
            gameOrder.setStatus(3001L);
            ywpdGameOrderService.updateYwpdGameOrder(gameOrder);
        }

    }

}

